package de.mariusharbeck.saralentz.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import de.mariusharbeck.saralentz.dao.AlbumDao;
import de.mariusharbeck.saralentz.entity.Album;

@Component("albumDao")
public class AlbumDaoImpl extends GenericDaoImpl<Album> implements AlbumDao {

	@Override
	public Album find(String name) {
		final Query query = this.em
				.createQuery("SELECT a FROM Album a WHERE LOWER(a.name) = :name");
		query.setParameter("name", name.toLowerCase());
		try {
			return (Album) query.getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Album> findLike(String name) {
		final Query query = this.em
				.createQuery("SELECT a FROM Album a WHERE LOWER(a.name) LIKE :name");
		query.setParameter("name", "%" + name.toLowerCase() + "%");
		return (List<Album>) query.getResultList();
	}

}
